<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .bg {
            width: 730px;
            height: 454px;
            margin: 20px auto;
            position: relative;
        }
        
        .bg img {
            width: 100%;
            height: 100%;
        }
        
        .left-btn {
            position: absolute;
            width: 30px;
            height: 40px;
            line-height: 40px;
            text-align: center;
            color: white;
            background-color: rgba(0, 0, 0, 0.5);
            left: 0;
            top: 50%;
            margin-top: -20px;
            cursor: pointer;
        }
        
        .right-btn {
            position: absolute;
            width: 30px;
            height: 40px;
            line-height: 40px;
            text-align: center;
            color: white;
            background-color: rgba(0, 0, 0, 0.5);
            right: 0;
            top: 50%;
            margin-top: -20px;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div class="bg">
        <img src="images/1.jpg" />
        <div class="left-btn">
            &lt;
        </div>
        <div class="right-btn">
            &gt;
        </div>
    </div>
    <script>
        var btnLeft = document.querySelector(".left-btn");
        var img = document.querySelector(".bg img");
        btnLeft.addEventListener("click", function(e) {
                var imageName = img.src;
                console.log(img.src);
                var arr = imageName.split(".");
                var indexArr = arr[arr.length - 2].split("/");
                console.log(indexArr);
                var index = indexArr[indexArr.length - 1];
                console.log(index);
                index = index - 1;
                if (index <= 0) {
                    index = 3;
                }
                console.log(index);
                img.src = "images/" + index + ".jpg";
            })
            //右边的
        var btnLeft = document.querySelector(".right-btn");
        var img = document.querySelector(".bg img");
        btnLeft.addEventListener("click", function(e) {
            var imageName = img.src;
            console.log(img.src);
            var arr = imageName.split(".");
            var indexArr = arr[arr.length - 2].split("/");
            console.log(indexArr);
            var index = indexArr[indexArr.length - 1];
            console.log(index);
            index = Number(index) + 1;
            if (index > 3) {
                index = 1;
            }
            console.log(index);
            img.src = "images/" + index + ".jpg";
        })
    </script>
</body>

</html>